<?php

namespace plugin\webman\lucky;
use support\Db;
use GatewayWorker\Lib\Gateway;
use support\Redis;
use plugin\webman\common\Common;


class Lucky
{

    public static function timer(){
        $time = 60-date('s',time());

    }
    public static function enterroom($client_id,$data){
        // 总奖池金额
        $info['jiangchi'] = Db::table('wa_jiangchi_lucky')->sum('amount');
        // 当前期号
        $info['expect'] = Common::getlucky_expect();
        $info['userinfo'] = Common::getuserinfo($data['uid']);
        // 每12分钟自动回复一次挑战次数
        $info['bet']['userallbet']=(string)Db::table("wa_touzhu_lucky")->where(['expect'=>$info['expect'],'uid'=>$data['uid']])->count();
        $info['bet']['allbet']='5';  //每12分钟可投注几次
        
        $data=[
            'code'=>1,
            'msg'=>'success',
            'info'=>$info
        ];
        return $data;
    }

    public static function touzhu($client_id,$data){
        $expect = Common::getlucky_expect();
        // 判断机会是否用完
        $allcount = Db::table("wa_touzhu_lucky")->where(['uid'=>$data['uid'],'expect'=>$expect])->count();
        if($allcount>=5){
            $data=[
                'code'=>1050,
                'msg'=>'本期挑战次数已用完'
            ];
            return $data;
        }
        $tz[0]=rand(0,9);
        $tz[1]=rand(0,9);
        $tz[2]=rand(0,9);
        // 插入投注记录

        switch ($tz[2]) {
            case '0':
                // 瓜分奖池5%的奖金
                $jiangchi = Db::table('wa_jiangchi_lucky')->sum('amount');
                $reward = $jiangchi * 0.05;
                $fanhuan = 0;
                $ifwin = 1;
                $remsg = '恭喜您瓜分奖池'.$reward.'积分';
                break;
            case '1':
                $reward = 0;
                $fanhuan = 500*0.2;
                $ifwin = 2;
                $remsg = '返还'.$fanhuan.'积分';
                break;    
            case '2':
                $reward = '0';
                $fanhuan = 500*0.2;
                $ifwin = 2;
                $remsg = '返还'.$fanhuan.'积分';
                break;
            case '3':
                $reward = '0';
                $fanhuan = 500*0.3;
                $remsg = '返还'.$fanhuan.'积分';
                $ifwin = 2;
                break;  
            case '4':
                $reward = '0';
                $fanhuan = 500*0.4;
                $ifwin = 2;
                $remsg = '返还'.$fanhuan.'积分';
                break;
            case '5':
                $reward = '0';
                $fanhuan = 500*0.5;
                $ifwin = 2;
                $remsg = '返还'.$fanhuan.'积分';
                break;    
            case '6':
                $reward = '0';
                $fanhuan = 500*0.6;
                $ifwin = 2;
                $remsg = '返还'.$fanhuan.'积分';
                break;
            case '7':
                $reward = '0';
                $fanhuan = 500*0.7;
                $ifwin = 2;
                $remsg = '返还'.$fanhuan.'积分';
                break;    
            case '8':
                $reward = '0';
                $fanhuan = 500*0.8;
                $ifwin = 2;
                $remsg = '返还'.$fanhuan.'积分';
                break;
            case '9':
                $reward = '0';
                $fanhuan = 500*0.9;
                $ifwin = 2;
                $remsg = '返还'.$fanhuan.'积分';
                break;               
           
        }

        $record = [
            'uid'=>$data['uid'],
            'tzcode'=>$tz[0].','.$tz[1].','.$tz[2],
            'weishu'=>$tz[2],
            'expect'=>$expect,
            'ifwin'=>1,
            'reward'=>$reward,
            'fanhuan'=>$fanhuan,
        ];
        Db::table('wa_touzhu_lucky')->insert($record);
        $info['bet']['userallbet']=(string)Db::table("wa_touzhu_lucky")->where(['expect'=>$expect,'uid'=>$data['uid']])->count();
        $info['bet']['allbet']='5';  //每12分钟可投注几次
        $info['result']=$tz[0].$tz[1].$tz[2];
        $info['result']=(int)$info['result'];
        $info['expect']=$expect;
        $info['remsg']=$remsg;
        $info['userinfo']=Common::getuserinfo($data['uid']);
        $info['userinfo']['jiangchi'] =Db::table('wa_jiangchi_lucky')->sum('amount');
        $res = [
            'code'=>1,
            'msg'=>'投注成功',
            'info'=>$info
        ];
        return $res;
    } 

}